package com.skp.shaphan.actions;

import java.awt.Color;

import com.skp.shaphan.timing.TimerAction;
import com.skp.shaphan.ui.SQLEditor;

public class HighlightSQLAction extends TimerAction {
	private SQLEditor editor;
	public HighlightSQLAction(SQLEditor editor) {
		this.editor = editor;
	}
	@Override
	public boolean isComplete() {
		Color target = editor.getBackgroundColor();
		Color actual = editor.getSQLBackgroundColor();
		if(target.getRGB() == actual.getRGB() && target.getAlpha() == actual.getAlpha()) {
			return true;
		} else {
			return false;
		}
	}
	@Override
	public void performAction() {
		Color target = editor.getBackgroundColor();
		Color actual = editor.getSQLBackgroundColor();
		
		int red = adjustComponent(actual.getRed(), target.getRed());
		int green = adjustComponent(actual.getGreen(), target.getGreen());
		int blue = adjustComponent(actual.getBlue(), target.getBlue());
		int alpha = adjustComponent(actual.getAlpha(), target.getAlpha());
		editor.setSQLBackgroundColor(new Color(red, green, blue, alpha));
		//System.out.println("Changing color... " + red + ", " + green + ", " + blue + ", " + alpha);
	}
	
	private int adjustComponent(int actual, int target) {
		int diff = target - actual;
		int adjustAmt = 20;
		if(Math.abs(diff) < adjustAmt) {
			adjustAmt = Math.abs(diff);
		}
		if(diff > 0) {
			return actual + adjustAmt;
		} else if(diff < 0) {
			return actual - adjustAmt;
		} else {
			return actual;
		}
	}
}
